//
//  LJFDetailCollectionCell.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/21.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFDetailCollectionCell.h"
#import "LJFDetailIntroduceTableCell.h"
#import "LJFDetailModel.h"
#import "Helper.h"
#import "Connector.h"
#import "LJFHttpManager.h"
#import "LJFIntroduceModel.h"
#import "LJFDetailLookCell.h"
#import "UIImageView+AFNetworking.h"
#import "LJFDetailServiceCell.h"
#import "LJFDetailHeadView.h"
#import "LJFCommentsModel.h"
#import "LJFCommentsCell.h"


@implementation LJFDetailCollectionCell

- (void)awakeFromNib
{
    // Initialization code
    
    [self creatTableView];
}

- (void)creatTableView
{
    CGFloat sceenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat sceenH = [UIScreen mainScreen].bounds.size.height;
    
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, sceenW, sceenH - 64 - 50) style:UITableViewStyleGrouped];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTableView.backgroundColor = [UIColor clearColor];
    [self addSubview:_myTableView];
    
    [_myTableView registerNib:[UINib nibWithNibName:@"LJFDetailIntroduceTableCell" bundle:nil] forCellReuseIdentifier:@"LJFDetailIntroduceTableCell"];
    
    [_myTableView registerNib:[UINib nibWithNibName:@"LJFDetailLookCell" bundle:nil] forCellReuseIdentifier:@"LJFDetailLookCell"];
    
    [_myTableView registerNib:[UINib nibWithNibName:@"LJFDetailServiceCell" bundle:nil] forCellReuseIdentifier:@"LJFDetailServiceCell"];
    [_myTableView registerNib:[UINib nibWithNibName:@"LJFCommentsCell" bundle:nil] forCellReuseIdentifier:@"LJFCommentsCell"];
    
    _refreshHead = [[MJRefreshHeaderView alloc] init];
    _refreshHead.scrollView = _myTableView;
    _refreshHead.delegate = self;
}

- (void)setDataDict:(NSDictionary *)dataDict
{
    _dataDict = dataDict;

    NSMutableArray *arrM = [NSMutableArray array];
    
    CGFloat width = self.frame.size.width - 100 - 10;
    
    for (NSDictionary *dict in dataDict[@"properties"])
    {
        NSString *value = dict[@"value"];
        CGFloat heigth = [Helper heightOfString:value font:[UIFont systemFontOfSize:15] width:width] + 10;
        
        LJFDetailModel *model = [[LJFDetailModel alloc] init];
        model.name = dict[@"name"];
        model.value = value;
        model.heigth = [NSString stringWithFormat:@"%f",heigth];
        
        [arrM addObject:model];
    }
    
    for (NSDictionary *dict in dataDict[@"brief"])
    {
        NSString *value = dict[@"value"];
        CGFloat heigth = [Helper heightOfString:value font:[UIFont systemFontOfSize:15] width:width] + 10;
        
        LJFDetailModel *model = [[LJFDetailModel alloc] init];
        
        model.name = dict[@"ti_title"];
        model.value = dict[@"value"];
        model.heigth = [NSString stringWithFormat:@"%f",heigth];

        
        [arrM addObject:model];
    }
    
    _dataArr = [NSArray arrayWithArray:arrM];
    
    [_myTableView reloadData];
}


- (void)setCollectionRow:(NSInteger)collectionRow
{
    _collectionRow = collectionRow;
    
    [_myTableView reloadData];
    
    if (collectionRow != 2)
    {
        _refreshHead.scrollView = nil;
    }
    else
    {
        _refreshHead.scrollView = _myTableView;
    }
    
    
    
}

#pragma mark -MJRefreshBaseViewDelegate

-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    [_refreshHead performSelector:@selector(endRefreshing) withObject:nil afterDelay:2.0];
}

- (void)dealloc
{
    [_refreshHead free];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_collectionRow == 1)
    {
        LJFIntroduceModel *model = _introduceArr[section];
        
        return model.childArr.count;
        
    }
    else if (_collectionRow == 2)
    {
        
        return  _commentArr.count;
    }
    else if(_collectionRow == 3)
    {
        LJFIntroduceModel *model = _LookArr[section];
        
        return model.childArr.count;
        
    }
    else if (_collectionRow ==4)
    {
    
        NSArray *arr = _serviceArr[section];
        
        return arr.count;
    }
    else
    {
        return _dataArr.count;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_collectionRow == 1)
    {
        return _introduceArr.count;
    }else if(_collectionRow == 3)
    {
        return _LookArr.count;
    }else if(_collectionRow == 4)
    {
        return 2;
    }
    else
    {
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   

    if (_collectionRow == 1)
    {
         LJFDetailIntroduceTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LJFDetailIntroduceTableCell"];
        
        LJFIntroduceModel *model = _introduceArr[indexPath.section];
        
        LJFDetailModel *model1 = model.childArr[indexPath.row];
        
        cell.contents.text = model1.value;
        cell.name.text = [NSString stringWithFormat:@"%@:",model1.name];
        
        return cell;
    }
    else if(_collectionRow == 2)
    {
        LJFCommentsModel *model = _commentArr[indexPath.row];
        
        LJFCommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LJFCommentsCell"];
        
        cell.contentH = model.contentH;
        cell.name.text = model.author;
        cell.time.text = model.create;
        
        cell.content.text = model.content;
        cell.reContent.text = model.re_content;
        
        
        if (model.head_pic.length > 0)
        {
            [cell.headImg setImageWithURL:[NSURL URLWithString:model.head_pic]];
        }
        else
        {
            cell.headImg.image = [UIImage imageNamed:@"default_image"];
        }
        
        cell.viewW.constant = (75 * [model.rank floatValue])/5.0;
        
        cell.picArr = model.pic_path;
        
        cell.recontentStr = model.re_content;
        
        return cell;
    }
    else if(_collectionRow == 3)
    {
        LJFIntroduceModel *model = _LookArr[indexPath.section];
        
        LJFDetailModel *model1 = model.childArr[indexPath.row];
        
        LJFDetailLookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LJFDetailLookCell"];
        [cell.imgView setImageWithURL:[NSURL URLWithString:model1.name] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        cell.title.text = model1.value;
        
        return cell;
    }
    else if(_collectionRow == 4)
    {
        NSArray *arr = _serviceArr[indexPath.section];
        NSDictionary *dict = arr[indexPath.row];
        
        LJFDetailServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LJFDetailServiceCell"];
        [cell.imgView setImageWithURL:[NSURL URLWithString:dict[@"goods_thumb"]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        cell.name.text = dict[@"goods_name"];
        cell.shopPrice.text = dict[@"shop_price"];
        cell.marketPrice.text = dict[@"market_price"];
        
        
        return cell;
    }
    else
    {
         LJFDetailIntroduceTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LJFDetailIntroduceTableCell"];
        
        LJFDetailModel *model = _dataArr[indexPath.row];
        cell.contents.text = model.value;
        cell.name.text = [NSString stringWithFormat:@"%@:",model.name];
        
        return cell;
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_collectionRow == 1)
    {
        LJFIntroduceModel *model = _introduceArr[indexPath.section];
        LJFDetailModel *model1 = model.childArr[indexPath.row];
        
        return [model1.heigth floatValue];
        
    }
    else if(_collectionRow == 2)
    {
        LJFCommentsModel *model = _commentArr[indexPath.row];
        
        return model.heigth_cell;
    }
    else if(_collectionRow == 3)
    {
        LJFIntroduceModel *model = _LookArr[indexPath.section];
        LJFDetailModel *model1 = model.childArr[indexPath.row];
        
        return [model1.heigth floatValue];
        
    }
    else if(_collectionRow == 4)
    {
        return 120;
    }
    else
    {
        LJFDetailModel *model = _dataArr[indexPath.row];
        
        NSString *str = model.heigth;
        
        return [str floatValue];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (_dataArr.count == 0 && _introduceArr.count == 0 && _commentArr.count == 0 &&_LookArr.count == 0 && _serviceArr.count == 0)
    {
        return nil;
    }
    
    LJFDetailHeadView *view = [[NSBundle mainBundle] loadNibNamed:@"LJFDetailHeadView" owner:self options:0][0];
    
    if (_collectionRow == 0)
    {
        view.title.text = @"服务简介";
    }
    else if (_collectionRow == 1)
    {
        LJFIntroduceModel *model = _introduceArr[section];
        
        view.title.text = model.title;
        
    }else if(_collectionRow == 2)
    {
        view.title.text = @"客户点评";
    }
    else if (_collectionRow == 3)
    {
        LJFIntroduceModel *model = _LookArr[section];
        
        view.title.text = model.title;
    }
    else if (_collectionRow == 4)
    {
        if (section == 0)
        {
            view.title.text = @"可能还需要";
        }
        
        if (section == 1)
        {
            view.title.text = @"大家还预订";
        }
    }
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_collectionRow == 4)
    {
        [_mydelegate Detailcell:self DidSelectedIndex:indexPath];
    }
}


@end
